<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register - AI French Teacher</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/french-theme.css') }}">
    <style>
        .auth-container {
            max-width: 400px;
            margin: 80px auto;
            padding: 40px;
            background: white;
            border-radius: 12px;
            box-shadow: var(--shadow-hover);
        }
        .auth-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .auth-header h1 {
            color: var(--primary-color);
            margin-bottom: 10px;
        }
        .auth-form .form-group {
            margin-bottom: 20px;
        }
        .auth-form label {
            display: block;
            margin-bottom: 5px;
            font-weight: 600;
            color: var(--text-color);
        }
        .auth-form input {
            width: 100%;
            padding: 12px;
            border: 2px solid var(--border-color);
            border-radius: 8px;
            font-size: 14px;
        }
        .auth-form input:focus {
            outline: none;
            border-color: var(--primary-color);
        }
        .auth-form button {
            width: 100%;
            padding: 12px;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            margin-top: 10px;
        }
        .auth-form button:hover {
            background: #357ABD;
        }
        .auth-form button:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        .auth-links {
            text-align: center;
            margin-top: 20px;
        }
        .error-msg, .success-msg {
            padding: 10px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
        }
        .error-msg {
            background: #FFEBEE;
            color: #C62828;
        }
        .success-msg {
            background: #E8F5E9;
            color: #2E7D32;
        }
        .password-hint {
            font-size: 12px;
            color: var(--text-light);
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="auth-container">
        <div class="auth-header">
            <div class="site-logo" style="justify-content: center; margin-bottom: 10px;">
                <span class="eiffel-tower">🗼</span>
                <h1 class="app-title" style="font-size: 32px;">AI French Teacher</h1>
            </div>
            <p style="color: #666;">Inscription • 注册新账号</p>
        </div>

        <div id="errorMsg" class="error-msg"></div>
        <div id="successMsg" class="success-msg"></div>

        <form class="auth-form" id="registerForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" placeholder="至少3个字符" required minlength="3">
            </div>

            <div class="form-group">
                <label for="email">邮箱</label>
                <input type="email" id="email" placeholder="your@email.com" required>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" placeholder="至少6个字符" required minlength="6">
                <div class="password-hint">密码至少6个字符</div>
            </div>

            <div class="form-group">
                <label for="confirmPassword">确认密码</label>
                <input type="password" id="confirmPassword" placeholder="再次输入密码" required>
            </div>

            <button type="submit" id="submitBtn">注册</button>
        </form>

        <div class="auth-links">
            <p>已有账号？ <a href="/login">立即登录</a></p>
        </div>

        <div style="margin-top: 30px; text-align: center;">
            <a href="/credits" style="color: var(--french-blue); text-decoration: none; font-size: 13px;">
                ⚜️ 致谢 / Remerciements
            </a>
        </div>
    </div>

    <script>
        const form = document.getElementById('registerForm');
        const errorMsg = document.getElementById('errorMsg');
        const successMsg = document.getElementById('successMsg');
        const submitBtn = document.getElementById('submitBtn');

        form.addEventListener('submit', async (e) => {
            e.preventDefault();

            const username = document.getElementById('username').value.trim();
            const email = document.getElementById('email').value.trim();
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            // 清除之前的消息
            errorMsg.style.display = 'none';
            successMsg.style.display = 'none';

            // 验证密码
            if (password !== confirmPassword) {
                errorMsg.textContent = '两次密码输入不一致';
                errorMsg.style.display = 'block';
                return;
            }

            // 禁用按钮
            submitBtn.disabled = true;
            submitBtn.textContent = '注册中...';

            try {
                const response = await fetch('/api/auth/register', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username, password, email }),
                    credentials: 'include'
                });

                const result = await response.json();

                if (result.success) {
                    successMsg.textContent = '注册成功！正在跳转到登录页面...';
                    successMsg.style.display = 'block';

                    // 2秒后跳转到登录页
                    setTimeout(() => {
                        window.location.href = '/login';
                    }, 2000);
                } else {
                    errorMsg.textContent = result.error;
                    errorMsg.style.display = 'block';
                    submitBtn.disabled = false;
                    submitBtn.textContent = '注册';
                }
            } catch (error) {
                errorMsg.textContent = '注册失败：' + error.message;
                errorMsg.style.display = 'block';
                submitBtn.disabled = false;
                submitBtn.textContent = '注册';
            }
        });

        // 实时验证密码一致性
        document.getElementById('confirmPassword').addEventListener('input', function() {
            const password = document.getElementById('password').value;
            const confirmPassword = this.value;

            if (confirmPassword && password !== confirmPassword) {
                this.style.borderColor = '#E74C3C';
            } else {
                this.style.borderColor = '';
            }
        });
    </script>
</body>
</html>

